package com.gopay.common.cps.dao.wallet.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.WaProductDao;
import com.gopay.common.domain.dps.WaProduct;
/**
 * 
 * @author dps
 *
 */
@Repository("waProductDao")
public class WaProductDaoImpl extends BaseDAOImpl<WaProduct, Integer> implements
		WaProductDao {

	@Override
	public List<WaProduct> getProductByType(String pType, boolean show) {

		Map<String, Object> param = new HashMap<String, Object>();

		String hql = "from WaProduct t where t.productType=:productType";
		param.put("productType", pType);
		if (show) {
			hql += " and t.isShow='1' and t.validityStatus='1'";
		}
		return this.findByHql(hql, param);
	}

	@Override
	public WaProduct getProductById(Integer productId) {
		return this.find(WaProduct.class, productId);
	}
	@Override
	public List<WaProduct> queryBy3Condition(String productType ,String operator ,String productAmt,String suppliersNo 
			,String isShow ,String validityStatus ,String id) {

		Map<String, Object> param = new HashMap<String, Object>();

		String hql = "from WaProduct t where  t.productType=:productType and t.operator=:operator and t.productAmt=:productAmt ";
		param.put("operator", operator);
		param.put("productType", productType);
		param.put("productAmt", new BigDecimal(productAmt));
		if(validityStatus != null && !"".equals(validityStatus)){
			hql += " and t.validityStatus=:validityStatus ";
			param.put("validityStatus", validityStatus);
		}else{
			hql += " and t.validityStatus !='0' ";
		}
		
		if(suppliersNo != null && !"".equals(suppliersNo)){
			hql += " and t.suppliersNo=:suppliersNo ";
			param.put("suppliersNo", suppliersNo);
		}
		
		if ( isShow != null && !"".equals(isShow)) {
			hql += " and t.isShow=:isShow ";
			param.put("isShow", isShow);
		}
		if ( id != null && !"".equals(id)) {
			hql += " and t.id!=:id ";
			param.put("id", Integer.valueOf(id));
		}
		return this.findByHql(hql, param);
	}
	@Override
	public List<WaProduct> queryBy6Condition(String productType ,String operator ,String useRange , String rechargeProvince 
			,String productValue,String suppliersNo ,String isShow ,String validityStatus ,String id){
		Map<String, Object> param = new HashMap<String, Object>();

		String hql = "from WaProduct t where  t.productType=:productType and t.operator=:operator and t.useRange=:useRange and t.rechargeProvince=:rechargeProvince and t.productValue=:productValue ";
		param.put("operator", operator);
		param.put("productType", productType);
		param.put("useRange", useRange);
		param.put("rechargeProvince", rechargeProvince);
		param.put("productValue", productValue);
		
		if(validityStatus != null && !"".equals(validityStatus)){
			hql += " and t.validityStatus=:validityStatus ";
			param.put("validityStatus", validityStatus);
		}else{
			hql += " and t.validityStatus !='0' ";
		}
		
		if(suppliersNo != null && !"".equals(suppliersNo)){
			hql += " and t.suppliersNo=:suppliersNo ";
			param.put("suppliersNo", suppliersNo);
		}
		
		if ( isShow != null && !"".equals(isShow)) {
			hql += " and t.isShow=:isShow ";
			param.put("isShow", isShow);
		}
		if ( id != null && !"".equals(id)) {
			hql += " and t.id!=:id ";
			param.put("id", Integer.valueOf(id));
		}
		return this.findByHql(hql, param);
	}
	
	@Override
	public List<WaProduct> queryByHaveBecomeValidity(String haveBecomeValidity ){
		Map<String, Object> param = new HashMap<String, Object>();
		String hql = "from WaProduct t where  t.haveBecomeValidity=:haveBecomeValidity ";
		param.put("haveBecomeValidity", haveBecomeValidity);
		return this.findByHql(hql, param);
	}

	@Override
	public List<WaProduct> queryProductByCondtion(Map<String, Object> params) {
		String hql = "from WaProduct t where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND "+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql, params);
	}
}


